{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4b1d02c1",
   "metadata": {},
   "source": [
    "# 字典的初始"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1e9a1b",
   "metadata": {},
   "source": [
    "## 字典的文档\n",
    "（https://docs.python.org/3/tutorial/datastructures.html#dictionaries）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "704a605f",
   "metadata": {},
   "source": [
    "## 字典的属性\n",
    "> 结构：key：value（键值对）\n",
    "> 字典是无序的，我们通过健(key)去查找值()\n",
    "> 字典对key的定义有一定的要求，key是由“不可变”的数据结构才可以定义（list是不能作为key来定义的）\n",
    "> 字典的key是唯一的,字典的值value也可以不唯一。（list的值是唯一的么？）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9874b6d0",
   "metadata": {},
   "source": [
    "## 字典的创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac342b22",
   "metadata": {},
   "source": [
    "### 无序的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ce92521e",
   "metadata": {},
   "outputs": [],
   "source": [
    "users = [\n",
    "    [\"zhichao\",23,\"IT\",20000],\n",
    "    [\"xiaoming\",20,\"IT\",21000]\n",
    "]\n",
    "# 试对比：\n",
    "users_dict = {\n",
    "    \"zhichao\":[23,\"IT\",20000],\n",
    "    \"xiaoming\":[20,\"IT\",21000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "41a8ea7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xiaoming', 20, 'IT', 21000]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询信息xiaoming：\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "bfb70b4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 'IT', 21000]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"xiaoming\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "affe135f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 试问:如果users的数据很大，想查找其中一个信息，但不知道其索引值\n",
    "# 这时list就变得很无力，而dict只需要知道数据的关键信息即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5856eb11",
   "metadata": {},
   "source": [
    "### 字典的创建(常见的三种方式)\n",
    "> info = {key1:value1 ,key2:value2,...}直接按照字典格式创建\n",
    "> info_02 = dict(key1=value1,key2=value2,key3=value3)使用dict()进行字典创建\n",
    "> info_03 = {}info_03[key1]=value1,info_03[key2]=value2 运用字典增加的方式新建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4345a2e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'sehun', 'stu10002': 'luhan', 'stu10003': 'chen'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "- 1\n",
    "info = {\n",
    "    \"stu10001\":\"sehun\",\n",
    "    \"stu10002\":\"luhan\",\n",
    "    \"stu10003\":\"chen\"\n",
    "}\n",
    "print(info)\n",
    "print(type (info))  # 查看info的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3e70e9ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiumin', 'stu10002': 'kai', 'stu10003': 'lay'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "- 2\n",
    "info_02=dict(\n",
    "    stu10001=\"xiumin\",\n",
    "    stu10002=\"kai\",\n",
    "    stu10003=\"lay\"\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d563ff3a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'baekhyun', 'stu10002': 'chanyeol', 'stu10003': 'kris'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "- 3\n",
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"] = \"baekhyun\"\n",
    "info_03[\"stu10002\"] = \"chanyeol\"\n",
    "info_03[\"stu10003\"] = \"kris\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0386bed4",
   "metadata": {},
   "source": [
    "# 字典的增、删、改、查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feb18a4f",
   "metadata": {},
   "source": [
    "## 字典的增加（和字典的第三种创建方式一致）：\n",
    "info_03[\"stu10004\"] =\"Tao\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c3b5ebc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'baekhyun', 'stu10002': 'chanyeol', 'stu10003': 'kris', 'stu10004': 'Tao'}\n"
     ]
    }
   ],
   "source": [
    "# 增加一个值 再循环列表\n",
    "info_03 [\"stu10004\"] = \"Tao\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07e9e099",
   "metadata": {},
   "source": [
    "## 字典的查询\n",
    "info_03[key name]\n",
    "注意：正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    "key_name info_03 查看是否存在？\n",
    "如果存在，返回结果为True，不存在为False\n",
    "info_03[key name]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "d889b0b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sehun\n"
     ]
    }
   ],
   "source": [
    "# 字典的查询\n",
    "print(info['stu10001'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "e9a78ac0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'kris'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用key查到value的数据\n",
    "info_03[\"stu10003\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "89680822",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "查询方法不存在\n"
     ]
    }
   ],
   "source": [
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询方法不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "4cd6f6bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'baekhyun', 'stu10002': 'chanyeol', 'stu10003': 'kris', 'stu10004': 'Tao', 'stu_03': 'Alex'}\n"
     ]
    }
   ],
   "source": [
    "# 修改:也要先查询key\n",
    "if \"stu10003\" in info_03:\n",
    "    info_03 [\"stu_03\"] = \"Alex\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2c8e357",
   "metadata": {},
   "source": [
    "## 字典的修改\n",
    "1.查询key是否存在\n",
    "2.修改key的值\n",
    "例如：修改info_03中stu10003的值为kris，if \"stu10003\"in info_03\n",
    "[\"stu10003\"]=\"kris\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "5725d355",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'sehun',\n",
       " 'stu10002': 'luhan',\n",
       " 'stu10003': 'chen',\n",
       " '100': 'jennie',\n",
       " '101': 'jackson',\n",
       " '102': 'krystal'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的修改\n",
    "info[\"100\"] = \"jessi\"\n",
    "info[\"101\"] = \"jackson\"\n",
    "info[\"102\"] = \"krystal\"\n",
    "info[\"100\"] = \"jennie\"\n",
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17a87511",
   "metadata": {},
   "source": [
    "## 字典的删除\n",
    "pop(key)\n",
    "popitem()\n",
    "del dict['stu10003'] # 删除键是'stu10003'的条目\n",
    "dict.clear() # 清空字典所有条目\n",
    "del dict # 删除字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "e5d767c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'101': 'jackson', '102': 'krystal', '103': 'krystal', '100': 'jessi'}\n",
      "{'101': 'jackson', '102': 'krystal', '103': 'krystal'}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除删    list->pop   dict -> del  \n",
    "# 删除全部   dict -.clear()\n",
    "info[\"100\"] = \"jessi\"\n",
    "info[\"101\"] = \"jackson\"\n",
    "info[\"102\"] = \"krystal\"\n",
    "print(info)\n",
    "del info[\"100\"]\n",
    "print(info)\n",
    "info.clear()\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "555766ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'baekhyun', 'stu10002': 'chanyeol', 'stu10003': 'kris', 'stu10004': 'Tao', 'stu_03': 'Alex'}\n"
     ]
    }
   ],
   "source": [
    "info_03[\"stu10004\"] = \"Tao\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "7dced51c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'kris'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用key查到value的数据\n",
    "info_03[\"stu10003\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "15fd4c07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "查询的key不存在\n"
     ]
    }
   ],
   "source": [
    "# 正确查询字典的方式\n",
    "print(\"stu10003\"in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "5f7f323c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'baekhyun', 'stu10002': 'chanyeol', 'stu10003': 'kris', 'stu10004': 'Tao', 'stu_03': 'Alex'}\n"
     ]
    }
   ],
   "source": [
    "# 修改： 也要查询key\n",
    "if \"stu10003\" in info_03:\n",
    "    info_03[\"stu10003\"] = \"kris\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "fb46e885",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'baekhyun', 'stu10002': 'chanyeol', 'stu10004': 'Tao', 'stu_03': 'Alex'}\n"
     ]
    }
   ],
   "source": [
    "# 字典的删除\n",
    "info_03.pop(\"stu10003\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5046a8aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01eb52dd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
